﻿using System;
using System.Runtime.Serialization;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

namespace UD.Utilities.Exceptions
{
    public class GenericException : Exception, ISerializable
    {
        public GenericException()
        {
        }

        public GenericException(string message)
            : base(message)
        {
        }

        public GenericException(string message, Exception inner)
            : base(message, inner)
        {
            bool flag = inner as SqlException == null;
            if (flag)
            {
                throw new UserInterfaceException(inner.Message, inner);
            }
            else
            {
                SqlException sqlException = (SqlException)inner;
                int number = sqlException.Number;
                if (number == 530)
                {
                    throw new UserInterfaceException("Recursividad invalida, por favor verifique la estructura, verifique por favor.");
                }
                else
                {
                    if (number == 547)
                    {
                        throw new UserInterfaceException("El registro tiene elementos que dependen de él, verifique por favor.");
                    }
                    else
                    {
                        if (number == 2601)
                        {
                            throw new UserInterfaceException("Registro duplicado, verifique por favor.");
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }
        }

        public GenericException(Exception inner)
        {
            string str;
            bool flag = inner as SqlException == null;
            if (flag)
            {
                str = this.CrearLog(inner);
                flag = !(str == "");
                if (flag)
                {
                    throw new UserInterfaceException(string.Concat("Por favor consulte a soporte tecnico, error: ", str), inner);
                }
                else
                {
                    throw new UserInterfaceException(inner.Message, inner);
                }
            }
            else
            {
                SqlException sqlException = (SqlException)inner;
                int number = sqlException.Number;
                if (number == 530)
                {
                    throw new UserInterfaceException("Recursividad invalida, por favor verifique la estructura, verifique por favor.");
                }
                else
                {
                    if (number == 547)
                    {
                        throw new UserInterfaceException("El registro tiene elementos que dependen de él, verifique por favor.");
                    }
                    else
                    {
                        if (number == 2601)
                        {
                            throw new UserInterfaceException("Registro duplicado, verifique por favor.");
                        }
                        else
                        {
                            str = this.CrearLog(inner);
                            flag = !(str == "");
                            if (flag)
                            {
                                throw new UserInterfaceException(string.Concat("Por favor consulte a soporte tecnico, error: ", str), inner);
                            }
                            else
                            {
                                throw new UserInterfaceException(inner.Message, inner);
                            }
                        }
                    }
                }
            }
        }

        protected GenericException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        private string CrearLog(Exception inner)
        {
            string str = "";
            bool item = ConfigurationManager.AppSettings["LogPath"] == null;
            if (!item)
            {
                item = !Directory.Exists(ConfigurationManager.AppSettings["LogPath"]);
                if (!item)
                {
                    try
                    {
                        Guid guid = Guid.NewGuid();
                        str = guid.ToString();
                        string str1 = string.Concat(ConfigurationManager.AppSettings["LogPath"], str, ".txt");
                        FileStream fileStream = new FileStream(str1, FileMode.OpenOrCreate, FileAccess.Write);
                        StreamWriter streamWriter = new StreamWriter(fileStream);
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine("Fecha Excepcion:");
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        DateTime now = DateTime.Now;
                        streamWriter.WriteLine(now.ToString());
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine("Mensaje Excepcion:");
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine(inner.Message);
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine("StackTrace Excepcion:");
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine(inner.StackTrace);
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine("Source Excepcion:");
                        streamWriter.WriteLine("---------------------------------------------------------------------------------");
                        streamWriter.WriteLine(inner.Source);
                        streamWriter.Close();
                    }
                    catch
                    {
                        str = "";
                    }
                }
            }
            string str2 = str;
            return str2;
        }
    }
}
